package cn.jee.controller;

import cn.jee.entity.Stu;
import cn.jee.repository.StuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/stu")
public class StuController {
  @Autowired
  StuRepository repository;
  @Autowired
  private StuRepository stuRepository;

  @ResponseBody //把返回值转换为json
  @RequestMapping("/all")
  public List<Stu> all(){
    return repository.findAll();
  }
  @ResponseBody
  @RequestMapping("/{id}")
  public Stu findById(@PathVariable long id){
    return repository.findById(id).orElse(null);
  }
  @RequestMapping("/page")
  public String page(@PageableDefault(size=2) Pageable pageable, Model model){
    Page<Stu> stus=stuRepository.findAll(pageable);
    model.addAttribute("stus",stus);
    return "stu_page";
  }
}
